﻿using HidApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySoftLab.DapFlashCore.Probes.DapAccess.Interfaces
{
    public class MyDevice : IDisposable
    {
        public ushort Vid { get; set; }

        public ushort Pid { get; set; }

        public string Path { get; set; }

        public string SerialNumber { get; set; }

        public Device? Device { get; set; }

        public MyDevice(ushort vid, ushort pid, string path, string serialNumber)
        {
            Vid = vid;
            Pid = pid;
            Path = path;
            SerialNumber = serialNumber;
        }

        public void OpenPath()
        {
            if (Device != null)
            {
                Device.Dispose();
            }

            Device = new Device(Path);
        }

        public byte[] Read(int maxLength)
        {
            if (Device == null)
            {
                return new byte[0];
            }

            return Device.Read(maxLength).ToArray();
        }

        public void Write(byte[] data)
        {
            if (Device != null)
            {
                Device.Write(data);
            }
        }

        public void Dispose()
        {
            Device?.Dispose();
        }
    }
}
